﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace GTP.AppFrame.PrototypeTools.Common
{
    /// <summary>
    /// 搜索查询相关的类
    /// </summary>
    public static class FindUtility
    {
        /// <summary>
        /// 设置当前登录的Cookie值
        ///  类似：ASP.NET_SessionId=wddn4tzixpnemsikre0uo2bk; GTP_IdServer_LangID=2052; .ASPXAUTH=F94276972FFAD5A3E9BFB7F133552663BBAC9B9BD5C5ED0A07CD70F0C4F954AFA67FBE7F17C23CD6E7487BEE73900BA1599C8FB38C496D9F301E25EB02EE6A7062189FFBFF00681EEB0A622F499F033827A06FBC388C9169109164B5D892E61F9730B9BF9951ADA2327C09EFE6A44A56954D99328D8B794AA9E5063A8A820A39F10E3F33D3B8E79D93DE779860BCFE32; 0_styleName=styleA
        /// </summary>
        private static string CurrentCookie = string.Empty;

        /// <summary>
        /// 获取jsx页的控件标签
        /// FieldLabel="单据编号"
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FindLabel(string data)
        {
            string result = string.Empty;
            string str = string.Empty;
            string pattern = "FieldLabel=\"[^\"]*\"";   //主实体
            string patternSub = "Header=\"[^\"]*\"";    //子实体
            //先判断data是jsx内容还是url
            if (data.Contains("http://") && data.Contains(".aspx"))
            {
                //如果是url，从url中获取jsx文件的内容赋值为data
                data = GetJsxContent(data);
                pattern = "fieldLabel: \"[^\"]*\"";
                patternSub = "header: \"[^\"]*\"";
            }
            Regex reg = new Regex(pattern);
            MatchCollection list = reg.Matches(data);
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    str = item.ToString().Replace("FieldLabel=", "").Replace("\"", "").Replace("fieldLabel: ", "");
                    result += str + "\r\n";
                }
            }
            result += "==================\r\n";
            //子实体
            reg = new Regex(patternSub);
            list = reg.Matches(data);
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    str = item.ToString().Replace("Header=", "").Replace("\"", "").Replace("header: ", "");
                    if (str != "false")
                    {
                        result += str + "\r\n";
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 输出jsx文件
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetJsxContent(string url)
        {
            string result = string.Empty;
            //伪造登录
            //string cookie = "ASP.NET_SessionId=wddn4tzixpnemsikre0uo2bk; GTP_IdServer_LangID=2052; .ASPXAUTH=F94276972FFAD5A3E9BFB7F133552663BBAC9B9BD5C5ED0A07CD70F0C4F954AFA67FBE7F17C23CD6E7487BEE73900BA1599C8FB38C496D9F301E25EB02EE6A7062189FFBFF00681EEB0A622F499F033827A06FBC388C9169109164B5D892E61F9730B9BF9951ADA2327C09EFE6A44A56954D99328D8B794AA9E5063A8A820A39F10E3F33D3B8E79D93DE779860BCFE32; 0_styleName=styleA";
            string cookie = string.Empty; ;
            if (!string.IsNullOrEmpty(CurrentCookie))
            {
                cookie = CurrentCookie;
            }
            else
            {
                if (url.Contains("cookie="))
                {
                    cookie = url.Substring(url.IndexOf("cookie=")).Replace("cookie=", "");
                    CurrentCookie = cookie;
                }
            }
            Uri uri = new Uri(url);
            url = uri.AbsoluteUri.Replace(".aspx", ".jsx") + "&culture=zh_CN";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Headers.Add("Cookie", cookie);
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            if (res.ResponseUri.AbsoluteUri.Contains("login.ashx"))
            {
                throw new Exception("请先登记Cookie！");
            }
            StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
            result = sr.ReadToEnd();
            sr.Close();
            res.Close();
            return result;
        }
    }
}
